/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.loja.view.swing.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;

/**
 *
 * @author CASA
 */
public class EntityComboBoxModel<E> extends AbstractListModel<E> implements
        ComboBoxModel<E> {

    private static final long serialVersionUID = 1L;

    private List<E> entities = Collections.EMPTY_LIST;
    private Object selectedObject;

    public EntityComboBoxModel(List<E> entities) {
        if (entities != null) {
            this.entities = entities;
        }
    }

    @Override
    public int getSize() {
        return entities.size();
    }

    @Override
    public E getElementAt(int index) {
        return entities.get(index);
    }

    @Override
    public void setSelectedItem(Object anItem) {
        if ((selectedObject != null && !selectedObject.equals(anItem))
                || selectedObject == null && anItem != null) {
            selectedObject = anItem;
        }
    }

    @Override
    public Object getSelectedItem() {
        return selectedObject;
    }

}
